﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EM.Repository.utility;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace EM.definition.finance.PaymentType
{
    public partial class _default : System.Web.UI.Page
    {
        int pageId = 16;
        DbHelper srv = new DbHelper();
        FillCombo fc = new FillCombo();

        protected void Page_Load(object sender, EventArgs e)
        {
            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(pageId, Authentication.RedirectionURLTypes.LoginPage);

            if (!Page.IsPostBack)
            {
                this.GetData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.GetData();
        }

        private void GetData()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT PAYMENTTYPEID, PAYMENTTYPENAME FROM TBLPAYMENTTYPE WHERE 1 = 1");

            SqlCommand cmd = new SqlCommand();

            if (txtPAYMENTTYPENAME.Text != "")
            {
                builder.Append(" AND INCOMETYPENAME LIKE @INCOMETYPENAME ");
                cmd.Parameters.AddWithValue("@INCOMETYPENAME", "%" + txtPAYMENTTYPENAME.Text + "%");
            }

            cmd.CommandText = builder.ToString();
            DataTable dt = srv.GetDataTable(cmd, "");
            rptBank.DataSource = dt;
            rptBank.DataBind();
        }


        protected void rptBank_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            TextBox txtPAYMENTTYPE = e.Item.FindControl("txtPAYMENTTYPE") as TextBox;
            if (e.CommandName == "Insert")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"INSERT INTO TBLPAYMENTTYPE
                                    (PAYMENTTYPENAME,CREUSERID, CREDATE, MODUSERID, MODDATE)
                                    SELECT @PAYMENTTYPENAME, @CREUSERID, GETDATE(), @CREUSERID, GETDATE()";
                cmd.Parameters.AddWithValue("@PAYMENTTYPENAME", txtPAYMENTTYPE.Text);
                cmd.Parameters.AddWithValue("@CREUSERID", Session["USERID"]);
                srv.ExecuteNonQuery(cmd);
            }
            else if (e.CommandName == "Update")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"UPDATE TBLPAYMENTTYPE SET
                                    PAYMENTTYPENAME = @PAYMENTTYPENAME,
                                    MODUSERID = @MODUSERID,
                                    MODDATE = GETDATE()
                                    WHERE PAYMENTTYPEID = @PAYMENTTYPEID";
                cmd.Parameters.AddWithValue("@PAYMENTTYPENAME", txtPAYMENTTYPE.Text);
                cmd.Parameters.AddWithValue("@MODUSERID", Session["USERID"]);
                cmd.Parameters.AddWithValue("@PAYMENTTYPEID", e.CommandArgument);
                srv.ExecuteNonQuery(cmd);
            }
            else if (e.CommandName == "Delete")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"DELETE TBLPAYMENTTYPE WHERE PAYMENTTYPEID = @PAYMENTTYPEID";
                cmd.Parameters.AddWithValue("@PAYMENTTYPEID", e.CommandArgument);
                srv.ExecuteNonQuery(cmd);
            }

            this.GetData();
        }

        protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                RequiredFieldValidator rfvNAME = e.Item.FindControl("rfvNAME") as RequiredFieldValidator;
                LinkButton btnUpdate = e.Item.FindControl("btnUpdate") as LinkButton;
                ValidationSummary vsUpdate = e.Item.FindControl("vsUpdate") as ValidationSummary;

                rfvNAME.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;
                btnUpdate.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;
                vsUpdate.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;
            }
        }
    }
}